<template>
  <!-- 操作日志 -->
  <div class="app-container table-flex-wrapper">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label prop="roleName">
        <el-input class="select1" v-model="queryParams.operName" placeholder="请输入用户名称" clearable size="small"/>
      </el-form-item>
      <el-form-item label prop="roleKey">
        <el-select class="select1" v-model="queryParams.roleName" placeholder="请选择角色类型" clearable size="small">
          <el-option v-for="dict in roleType" :key="dict.roleId" :label="dict.roleName" :value="dict.roleName" />
        </el-select>
      </el-form-item>
      <el-form-item label prop="status">
        <el-select class="select1" v-model="queryParams.title" placeholder="请选择操作模块" clearable size="small">
          <el-option v-for="dict in moduleOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictLabel" />
        </el-select>
      </el-form-item>
      <el-form-item label>
        <el-date-picker v-model="dateRange" size="small" style="width: 320px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" size="mini" :data="operationList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column prop="operId" label="序号" width="90" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="operName" label="用户名称" show-overflow-tooltip  align="center"></el-table-column>
      <el-table-column prop="roleName" label="角色类型" show-overflow-tooltip align="center"></el-table-column>
      <el-table-column prop="title" label="操作模块" width="300" align="center"></el-table-column>
      <el-table-column prop="businessType" label="动作内容" width="100" align="center" :formatter="typeFormat" show-overflow-tooltip></el-table-column>
      <el-table-column prop="operIp" label="IP地址" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="operTime" label="操作时间" :formatter="timeFormat" min-width="150" align="center" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" class="btn" @click="handleReview(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <review ref="review"></review>
  </div>
</template>

<script>
  import review from "./dialog/review";
  import { list, delOperlog, cleanOperlog, exportOperlog } from '@/api/monitor/operlog';
  import { getFormatTime, businessType } from '@/api/format.js';
  import { listRole } from "@/api/system/role";

  export default {
    name: "operate",
    components: {
      review
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 角色表格数据
        operationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示弹出层（数据权限）
        openDataScope: false,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        //模块列表
        moduleOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          operName: '',
          roleName: '',
          title: '',
          datetime: '',
          beginTime: '',
          endTime: '',
        },
        pickerOptions: {
          shortcuts: [{
            text: "最近一天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", [start, end]);
            }
          },
            {
              text: "最近三天",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 3);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              }
            }
          ]
        },
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 角色类型列表
        roleType: [],
        // 导出加载框
        exportLoading: {
          lock: true,
          text: '正在导出文件，请稍等',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        },
      };
    },
    created() {
      let self = this;
      self.getList();

      self.getDicts("sys_module").then(response => {
        self.moduleOptions = response.data;
      }).catch((error)=> {
        console.log(error);
      });

      listRole(null).then(response => {
        self.roleType = response.rows;
      }).catch((error)=> {
        console.log(error);
      });
    },
    methods: {
      // 查看
      handleReview(row) {
        let self = this;
        this.$refs.review.handleClick(row);
      },
      /** 查询角色列表 */
      getList() {
        let self = this;
        self.loading = true;
        list(self.addDateRange(self.queryParams, self.dateRange)).then(response=>{
          let result = response.rows.map(e => {
            try {
              e.operParam = JSON.parse(e.operParam);
            } catch(exception) {
              e.operParam = {};
            }
            return e;
          })
          self.operationList = result;
          self.total = response.total;
          self.loading = false;
        }).catch((error)=> {
          console.log(error);
        });
      },
      // 格式化业务类型
      typeFormat(row, column) {
        return businessType(row.businessType);
      },
      // 格式化时间
      timeFormat(row, column) {
        return getFormatTime(row.operTime);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        let self = this;
        self.getList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.roleId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 导出按钮操作 */
      handleExport() {
        let self = this;
        let loading = undefined;
        const queryParams = self.queryParams;
        self.$confirm("是否确认导出所有数据?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            loading = self.$loading(self.exportLoading);
            return exportOperlog(queryParams);
          }).then(response => {
            self.download(response.msg);
            self.msgSuccess("导出成功");
            loading.close();
          }).catch(function() {
            if (loading!=undefined) {
              loading.close();
            }
          });
      }
    }
  };
</script>
<style>
  .select1 {
    width: 180px;
    height: 32px;
  }
</style>
